// pages/index/index.js
var util = require('../../utils/util.js');
var localData = require('../../data/local-data.js');
var app = getApp();
var videoAd=null
Page({


  /**
   * 页面的初始数据
   */
  data: {
    guide:true,
    big_picture: false,
    circular: false,
    multipleNum: 6,
//  autoplay:true,
//   interval:2500,
    duration:1000,
//    circular:true,
  // skiphiddenitemlayout:true,  
    selectTag: 'all',  //默认选择全部
    selecttitle:'全部',
    categorys: [],
    showView: true,
    animationtwo: {},
    animationData: {},
    animationRotate: {},
    popupmenu: "hide",
    id: '',
    bind: 'onChangeShowState',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    // 在页面中定义激励视频广告


    // 在页面onLoad回调事件中创建激励视频广告实例
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-5020929588094641'
      })
      videoAd.onLoad(() => { })
      videoAd.onError((err) => { })
      videoAd.onClose((res) => { })
    }

    // 用户触发广告后，显示激励视频广告
    if (videoAd) {
      videoAd.show().catch(() => {
        // 失败重试
        videoAd.load()
          .then(() => videoAd.show())
          .catch(err => {
            console.log('激励视频 广告显示失败')
          })
      })
    }
    //加载本地数据
    // this.setData({
    //   categorys: localData.categorys
    // })
    let isIphoneX = app.globalData.isIphoneX;
    this.setData({
      isIphoneX: isIphoneX,
      categorys: localData.categorys
    })
  },

 /*   引导页   */

  guide: function () {
    this.setData({
      guide: false
    });
  }, 

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    var animation = wx.createAnimation({
      duration: 400,
       timingFunction: 'ease-in-out',
    })
    this.animation = animation
    var animations = wx.createAnimation({
      duration: 400,
       timingFunction: 'ease-in-out',
    })
    this.animations = animations
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      // 来自页面内转发按钮
      //console.log(res.target)
    }
    return {
      title: '拍照识物，尽在探头',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
        util.successToast("分享成功");
      },
      fail: function (res) {
        // 转发失败
        util.errorToast("分享取消");
      }
    }
  },

  /**
   * 装载图片
   */
  containers_img(event) {
    this.setData({
      big_picture: true
    })
  },
  
  /**
   * 点击选择
   */
  slideClick(e) {
    this.setData({
      selectTag: e.currentTarget.dataset.tag,
      selecttitle: e.currentTarget.dataset.title
     
    })
  //  console.log(selectTag);
  },
  
  /**
   * 打开相机
   */
  openCamera: function () {
    var that = this;
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var imageList = res.tempFilePaths;
        // 因为只选一张照片，所以获取第一张就可以
        var image = imageList[0];
        // 用户选择的分类标签
        var tag = that.data.selectTag;
        var title = that.data.selecttitle;
      //  console.log("img：" + image + " ---------- tag：" + tag);
      //  console.log(tag);
        wx.navigateTo({
          url: '../discern/discern?image=' + image + '&tag=' + tag + '&title=' + title,
        })
      },
    })
  },

  /**
 * 选择相册
 */
  chooseImage: function () {
    var that = this;
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var imageList = res.tempFilePaths;
        // 因为只选一张照片，所以获取第一张就可以
        var image = imageList[0];
        // 用户选择的分类标签
        var tag = that.data.selectTag;
        var title = that.data.selecttitle;
        //console.log("img：" + image + " ---------- tag：" + tag);
        wx.navigateTo({
          url: '../discern/discern?image=' + image + '&tag=' + tag + '&title=' + title,
        })
        console.log(title);
      },
    })
  },

  /**
   * 图片自动获取屏幕大小并改变事件
   */
  onChangeShowState: function (e) {
    var that = this;
    //var _this = this.data.animationRotate;
    var menu = "popup_menu";
    var hide = "hide";
    var id = e.currentTarget.id;
    // console.log(id)
    if (id != "one") {
      console.log(id)
      that.rotates();
      that.shrinkdown();
      that.shrinkdowntwo();

      that.setData({
        popupmenu: hide,
        bind: 'onbind'
      })
      //设置不可点击
      setTimeout(function () {
        that.setData({
          bind: 'onChangeShowState'
        })
      }.bind(this), 600)

    } else {
      that.rotate();
      that.rotateAndScale();
      that.rotatetwo();

      that.setData({
        bind: 'onbind'
      })
      setTimeout(function () {
        that.setData({
          bind: 'onChangeShowState'
        })
      }.bind(this), 600)
      setTimeout(function () {
        that.setData({
          popupmenu: menu,
        })
      }.bind(this), 350)
    }

    setTimeout(function () {
      that.setData({
        showView: (!that.data.showView)
      })
    }.bind(this), 180)
  },

  rotate: function () {
    this.animations.rotate(90).step();
    this.setData({
      animationRotate: this.animations.export()
    })
  },

  rotates: function () {
    this.animations.rotate(0).step();
    this.setData({
      animationRotate: this.animations.export()
    })
  },

  rotateAndScale: function () {
    // 放大内圈
    this.animation.scale(3, 3).step();
    this.setData({
      animationData: this.animation.export()
    })
  },

  rotatetwo: function () {
    // 放大外圈
    this.animation.scale(6, 6).step();
    this.setData({
      animationtwo: this.animation.export()
    })
  },

  shrinkdown: function () {
    this.animation.scale(1, 1).step();
    this.setData({
      animationData: this.animation.export()
    })
  },

  shrinkdowntwo: function () {
    this.animation.scale(1, 1).step();
    this.setData({
      animationtwo: this.animation.export()
    })
  },

  /**
   * 关于页面跳转事件
   */
  about: function () {
    wx.navigateTo({
      url: '../about/about'
    })
  },

  /**
   * 反馈页面跳转事件
   */
  feedback: function () {
    wx.navigateTo({
      url: '../feedback/feedback'
    })
  },

  /**
   * 免责页面跳转事件
   */
  impunity: function () {
    wx.navigateTo({
      url: '../impunity/impunity'
    })
  },
})